package com.woniuxy.controllers;

import com.woniuxy.entitys.Permission;
import com.woniuxy.entitys.RestPOJO;
import com.woniuxy.entitys.TreeNode;
import com.woniuxy.services.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "权限管理接口")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;


    @ApiOperation(
            value = "获取所有权限",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "key", value = "查询类型", required = false),
            @ApiImplicitParam(paramType = "query", name = "value", value = "查询内容", required = false),
    })
    //获取所有权限及children
    @GetMapping("/perstree")
    public RestPOJO getPers() {
        RestPOJO restPOJO = new RestPOJO();

        List<Permission> list = permissionService.getPersList();
        //System.out.println("$$$$$$$$$$$$$$$"+list);

        if (list != null && list.size() > 0) {
            restPOJO.setStatus(1000);
            restPOJO.setMessage("查询成功");
            restPOJO.setData(list);
        } else {
            restPOJO.setStatus(1001);
            restPOJO.setMessage("查询失败");
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "获取所有菜单权限",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //获取所有菜单权限
    @GetMapping("/menus")
    public RestPOJO getAllMenu() {
        List<Permission> menuList = permissionService.getMenuListWithoutRid();
        //System.out.println("$$$$$$$$"+menuList);
        RestPOJO restPOJO = new RestPOJO();
        if (menuList != null) {
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(menuList);

        } else {
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据用户的职位id获取权限",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "role_id", value = "职位id", required = true),
    })
    //根据用户角色id获取权限
    @GetMapping("/menus/{role_id}")
    public RestPOJO getMenu(@PathVariable("role_id") Integer role_id) {
        List<Permission> menuList = permissionService.getMenuList(role_id, 0);
        //System.out.println("$$$$$$$$"+menuList);
        RestPOJO restPOJO = new RestPOJO();
        if (menuList != null) {
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(menuList);

        } else {
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据查询语句查询相应权限",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "value", value = "查询内容", required = false),
    })
    @GetMapping("/pers")
    //根据查询语句查询相应权限
    public RestPOJO searchPers(String value) {
        RestPOJO restPOJO = new RestPOJO();
        if (value == null || value == "") {
            return getPers();
        } else {
            List<Permission> list = permissionService.searchPers(value);
            if (list != null && list.size() > 0) {
                restPOJO.setMessage("查询成功");
                restPOJO.setStatus(1000);
                restPOJO.setData(list);

            } else {
                restPOJO.setMessage("查询失败");
                restPOJO.setStatus(1001);
            }

        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据role_id获取权限树和该职位已有权限per_id",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "role_id", value = "职位id", required = true),
    })
    //获取所有权限treenode和根据role_id获取已有权限id
    @GetMapping("/perstree/{role_id}")
    public RestPOJO getPersTree(@PathVariable("role_id") Integer role_id) {
        RestPOJO restPOJO = new RestPOJO();
        //获取所有权限
        List<TreeNode> list = permissionService.getAllPers(0);
        //根据role_id获取已有权限id
        List<Integer> persIds = permissionService.getPersIdByRid(role_id);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("treeNodes", list);
        map.put("persIds", persIds);
        if (map != null) {
            restPOJO.setStatus(1000);
            restPOJO.setMessage("查询成功");
            restPOJO.setData(map);
        } else {
            restPOJO.setStatus(1001);
            restPOJO.setMessage("查询失败");
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据权限per_id修改权限状态",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "per_id", value = "权限id", required = true),
            @ApiImplicitParam(paramType = "query", name = "per_remark", value = "权限状态", required = true),
    })
    //修改权限状态
    @GetMapping("/pers/status")
    public RestPOJO updPer(Integer per_id, String per_remark) {
        RestPOJO restPOJO = new RestPOJO();
        Integer count = permissionService.updPerById(per_id, per_remark);
        if (count > 0) {
            restPOJO.setStatus(1000);
            restPOJO.setMessage("修改权限成功");
        } else {
            restPOJO.setStatus(1001);
            restPOJO.setMessage("修改权限失败");
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "保存新增菜单权限",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //保存新增菜单权限
    @PostMapping("/pers")
    public RestPOJO saveNewPer(@RequestBody Permission permission) {
        RestPOJO restPOJO = new RestPOJO();
        //默认权限状态为正常使用，类型为菜单
        permission.setPer_remark(1 + "");
        permission.setPer_type("menu");

        Integer count = permissionService.addPer(permission);
        if (count > 0) {
            restPOJO.setStatus(1000);
            restPOJO.setMessage("保存权限成功");
        } else {
            restPOJO.setStatus(1001);
            restPOJO.setMessage("保存权限失败");
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据职位role_id和权限per_id集合保存分配权限(数据格式如下{\"role_id\":5,\"perIds\":[\"1\",\"2\"]})",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    // 保存分配权限
    @PostMapping("/pers/role")
    public RestPOJO savePers(@RequestBody Map<String, Object> map) {
        Integer role_id = (Integer) map.get("role_id");
        ArrayList<Integer> perIds = (ArrayList<Integer>) map.get("perIds");
        RestPOJO restPOJO = new RestPOJO();
        System.out.println(role_id);
        System.out.println(perIds);
        Integer count = permissionService.savePers(role_id, perIds);
        if (count > 0) {
            restPOJO.setStatus(1000);
            restPOJO.setMessage("保存权限成功");
        } else {
            restPOJO.setStatus(1001);
            restPOJO.setMessage("保存权限失败");
        }
        return restPOJO;
    }


}
